const App = getApp()
var WxParse = require('../../../wxParse/wxParse.js');
const QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

const qqmapsdk = new QQMapWX({
  key: 'BUSBZ-SHGWX-K5E4U-7UBCD-3SS62-K6B5G' // 必填
});
Page({
  data: {
    show: false,
    isloding: false
  },
  onLoad: function (options) {
    var self = this;
    App.globalData.pm.then(res=>{
      self.getService()
      self.getSwiper()
      self.getStoreList()
      WxParse.wxParse('article', 'html', App.globalData.config.configList.store_index, self, 5);
    })
    
  },
  photo() {
    wx.navigateToMiniProgram({ appId: "wxf8a758957f77f91c" })
  },
  getStoreList(){
    var _this = this;
    App.globalData.userModel.storeList({
      data: {
        'contact_addr_lat': App.globalData.locationInfo.latitude,
        'contact_addr_lng': App.globalData.locationInfo.longitude
      }
    }).then(json => {
      if (json.data.list.length > 0) {
        json.data.list[0].check = true
      }
      _this.setData({ storeList: json.data.list })
    })
  },
  getSwiper() {
    var _this = this
    App.globalData.userModel.getSwiperList().then(json => {
      _this.setData({ swiperList: json.data })
    })
  },
  getService: function () {
    var _this = this
    App.globalData.serviceModel.service({}).then(json => {
      var devphotoinfo, cerphotoinfo, priphotoinfo=null;
      json.data.forEach((item)=>{
        switch(item.type){
          case 1: devphotoinfo = item; break;
          case 2: cerphotoinfo = item; break;
          case 3: priphotoinfo = item; break;
        }
      })
      _this.setData({
        devphotoinfo: devphotoinfo,
        cerphotoinfo: cerphotoinfo,
        priphotoinfo: priphotoinfo,
        storeInfo: App.globalData.curStore,
        isloding:true
      })
    })
    
  },
  selectStore(event) {
    const _this = this
    const{id} = event.currentTarget
    if (!_this.data.storeInfo||_this.data.storeInfo.id!=id){
      App.globalData.userModel.selectStore({ data: { st_id: id } }).then(json => {
        App.globalData.curStore = json.data
        _this.setData({ storeInfo: App.globalData.curStore })
        _this.getService()
        _this.getSwiper()
      })
    }
    this.setData({ show: false })
  },
  /**
   * 跳转充值页面
   */
  goRecharge: function () {
    wx.navigateTo({url: '/pages/index/recharge/recharge'});
  },
  chooseStore: function () {
    this.setData({ show: true})
  },
  closeStore: function () {
    this.setData({ show: false})
  }
})